#
#   Levels.py
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; version 2 of the License.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   Copyright
#   Author: Salvatore S. Gionfriddo
#   Contributors: Nicholas F. Hoover
#   Created: 2007.07.06
#   Last Modified: 2007.07.12
#

import Spawner
import Settings

from Level import Level

class Levels:
    (NAME, GOALS, LEVEL_BONUS, DESCRIPTION, SPAWNERS, BACKGROUND) = range(6)
    
    (SPAWNER_TYPE, SPAWNER_ARGS, SPAWN_FUNCTION, SPAWN_TICK) = range(4)
    
    (GOAL, GOAL_TYPE) = range(2)
    
    GOAL_DESCRIPTIONS = Settings.getGoals()
                         
    def __init__(self, statTrackers, spawnFunctions):
        self.statTrackers = statTrackers
        self.spawnFunctions = spawnFunctions
        
    def loadLevels(self, levels):
        i = 0
        self.levels = range(len(levels))
        for level in levels:
            #print i
            if len(level) == 6:
                self.levels[i] = Level(level[Levels.NAME], level[Levels.LEVEL_BONUS], level[Levels.DESCRIPTION], level[Levels.BACKGROUND])
            else:
                self.levels[i] = Level(level[Levels.NAME], level[Levels.LEVEL_BONUS], level[Levels.DESCRIPTION])
            for goal in level[Levels.GOALS]:
                goalStatTracker = self.statTrackers[goal[Levels.GOAL_TYPE]]
                goalType = Levels.GOAL_DESCRIPTIONS[goal[Levels.GOAL_TYPE]]
                goalTarget = goal[Levels.GOAL]
                self.levels[i].addGoal(goalTarget, goalStatTracker, goalType)
            for spawner in level[Levels.SPAWNERS]:
                spawnerFunction = self.spawnFunctions[spawner[Levels.SPAWN_FUNCTION]]
                spawnerArgs = spawner[Levels.SPAWNER_ARGS]
                statTracker = self.statTrackers[spawner[Levels.SPAWN_TICK]]
                if spawnerArgs != []:
                    spawner_obj = Spawner.__dict__[spawner[Levels.SPAWNER_TYPE]+'Spawner'](spawnerFunction, statTracker, *spawnerArgs)
                else:
                    spawner_obj = Spawner.__dict__[spawner[Levels.SPAWNER_TYPE]+'Spawner'](spawnerFunction, statTracker)
                self.levels[i].addSpawner(spawner_obj)
            i += 1
        
    def getLevels(self):
        return self.levels
